<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>商品管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
</head>
<body class="childrenBody">
<!-- 查询条件开始-->

<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form" >
            <div class="layui-form-item">
                <div class="layui-col-xs4">
                    <label class="layui-form-label" style="width: 100px;">供应商</label>
                    <div class="layui-input-inline">
                        <select name="providerid" id="search_providerid">
                            <option value="0">请选择供应商</option>
                        </select>
                    </div>
                </div>

                <div class="layui-col-xs4">
                    <label class="layui-form-label" style="width: 100px;">商品名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="gname" id="gname" class="layui-input searchVal" placeholder="请输入商品名称" />
                    </div>
                </div>

                <div class="layui-col-xs4" style="text-align: center;">
                    <button type="button" class="layui-btn layui-btn-radius" lay-submit="" lay-filter="doSearch" shiro:hasPermission="goods:view"><span class="layui-icon layui-icon-search"></span>查询</button>
                    <button type="reset" class="layui-btn layui-btn-warm layui-btn-radius"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </form>
    </blockquote>
</form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="goodsTable" lay-filter="goodsTable"></table>
    <!-- 工具栏 -->
    <div id="goodsToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm layui-btn-radius" shiro:hasPermission="goods:create">
            <span class="layui-icon layui-icon-add-1"></span>添加商品</button>
    </div>

    <!---->
    <div id="goodsRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm layui-btn-radius" shiro:hasPermission="goods:update"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger layui-btn-radius" shiro:hasPermission="goods:delete"><span class="layui-icon layui-icon-delete"></span>删除</button>
        <button type="button" lay-event="selectGoodsCategory" class="layui-btn layui-btn-radius layui-btn-sm" shiro:hasPermission="goods:selectCategory">
            <span class="layui-icon layui-icon-about"></span>分配类别
        </button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->

<div  style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">

        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-inline">
                <select name="providerid" id="add_providerid">
                    <option value="0">请选择供应商</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-block">
                <!--隐藏域-->
                <input type="hidden" name="gid">
                <input type="text" class="layui-input" name="gname" lay-verify="required" placeholder="请输入名称">
            </div>
        </div>

        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-col-xs6">
                <label class="layui-form-label">商品规格</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="size" lay-verify="required" placeholder="请输入规格">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">包装类型</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="goodspackage" lay-verify="required" placeholder="请输入包装类型">
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-col-xs6">
                <label class="layui-form-label">商品价格</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="gprice" lay-verify="required" placeholder="请输入价格">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">需要数量</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="dangerquantity" lay-verify="required" placeholder="请输入数量">
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn layui-btn-radius" lay-submit lay-filter="doSubmit" ><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-radius"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>

<!-- 添加和修改的弹出层结束 -->

<div style="display: none;padding: 5px" id="selectGoodsCategoryDiv">
    <table class="layui-hide" id="categoryTable" lay-filter="categoryTable"></table>
</div>

<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery','form','table','layer','laydate'],function () {
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var laydate=layui.laydate;

        //渲染表格
        var tableIns = table.render({
            elem: '#goodsTable'//绑定表格组件的id
            , toolbar: '#goodsToolBar'
            , height: 'full-110'
            , title: '商品数据表'
            , url: '/goods/goodsList/' //数据接口
            , page: true //开启分页
            , cols: [ [
                {field: 'gid', title: 'ID', align: 'center', width: '8%'}
                , {field: 'gname', title: '商品名称', align: 'center', width: '14%'}
                , {field: 'gnumbering', title: '商品编号', align: 'center', width: '14%'}
                , {field: 'providername', title: '供应商', align: 'center', width: '14%'}
                , {field: 'size', title: '商品规格', align: 'center', width: '8%'}
                , {field: 'goodspackage', title: '商品包装', align: 'center', width: '8%'}
                , {field: 'gprice', title: '商品价格', align: 'center', width: '8%'}
                , {field: 'gquantity', title: '库存数量', align: 'center', width: '8%'}
                , {field: 'dangerquantity', title: '需要货物量', align: 'center', width: '8%'}
                , {field: 'right', title: '操作', align: 'center', width: '25%',toolbar: "#goodsRowBar"}
            ] ]
            //回调函数
            , done: function (res,curr, count) {
                if (curr > 1 && res.data.length == 0) {
                    var pageValue = curr -1;
                    tableIns.reload({
                        page:{curr:pageValue}
                    })
                }
            }
        });

        //供货商下拉框加载
        $.get("provider/loadAllProvider",function (result) {
            var data=result.data;
            var dom= $("#search_providerid");
            var html = '<option value="0">请选择供应商</option>'
            $.each(data,function (index,item) {
                html+='<option value="'+item.id+'">'+item.providername+'</option>'
            });
            dom.html(html);
            form.render("select");

        });
        $.get("provider/loadAllProvider",function (result) {
            var data=result.data;
            var dom= $("#add_providerid");
            var html = '<option value="0">请选择供应商</option>'
            $.each(data,function (index,item) {
                html+='<option value="'+item.id+'">'+item.providername+'</option>'
            });
            dom.html(html);
            form.render("select");

        });

        //搜索
        form.on("submit(doSearch)", function (data) {
            //表格数据重新加载
            console.log(data);
            tableIns.reload({
                where: data.field //额外的参数
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //关闭跳转页面
        });

        //监听添加按钮
        table.on('toolbar(goodsTable)', function (obj) {
            switch (obj.event) {
                //添加栏触发事件
                case 'add':
                    openAddGoodsWindow();//添加方法
                    break;
            }
        });

        var mainIndex;//弹出层的索引下标
        var url;//提交路径

        //弹出添加框函数
        function openAddGoodsWindow() {
            mainIndex = layer.open({
                type:1,
                title:"添加商品",
                area:["600px","350px"],
                content:$("#addOrUpdateDiv"),//内容窗口
                success:function () {
                    //每次开启清空弹出框数据
                    $("#dataFrm")[0].reset();
                    url="/goods/addgoods";
                }
            });
        }

        //监听添加和修改窗口的提交按钮
        form.on("submit(doSubmit)", function (data) {
            //表格数据重新加载
            console.log(data);
            $.post(url,data.field,function (result) {
                //判断是否成功
                if (result.status){
                    //成功表格刷新
                    tableIns.reload();
                }
                //弹出是否成功消息
                layer.msg(result.message);
                //关闭窗口
                layer.close(mainIndex)
            },"json");
            return false; //关闭跳转页面
        });

        //监听修改和删除按钮
        table.on("tool(goodsTable)", function (obj) {
            var data =obj.data;
            switch (obj.event) {
                //修改数据
                case 'update':
                    openUpdateGoodsWindow(data);
                    break;
                case 'delete':
                    deleteGoods(data);
                    break;
                case 'selectGoodsCategory':
                    selectGoodsCategory(data);
                    break;
            }
        });

        //分配角色
        function selectGoodsCategory(data){
            mainIndex = layer.open({
                type:1,
                title:"分配"+data.gname+"的类别",
                area:["500px","400px"],
                content:$("#selectGoodsCategoryDiv"),//内容窗口
                btn: ['确定', '取消'],
                btnAlign: 'c',
                yes: function (index, layero) {
                    //获取所有选中节点
                    var checkData = table.checkStatus('categoryTable');//获取选中行的数据
                    if (checkData.data.length==0){
                        layer.msg("未选中")
                    }else{
                        console.log(checkData.data)
                        var idArr =[];
                        for (var i=0;i<checkData.data.length;i++){
                            idArr.push(checkData.data[i].cateid);
                        }
                        var ids = idArr.join(",");
                        $.post("/goods/saveGoodsCategory",{"categoryids":ids,"goodsid":data.gid},function (result) {
                            layer.msg(result.message)
                        },"json");
                        layer.close(index);//关闭提示框
                    }
                },
                btn2: function (index, layero) {
                },
                success:function () {
                    initCategoryTable(data);
                }
            });
        }


        //初始化角色列表
        function initCategoryTable(data){
            table.render({
                elem: '#categoryTable'//绑定表格组件的id
                , url: '/goods/initGoodsByCategoryId/' //数据接口
                ,where:{
                    "id":data.gid
                }
                , cols: [ [
                    {type: 'checkbox', fixed: 'center'}
                    , {field: 'cateid', title: 'ID', align: 'center'}
                    , {field: 'catename', title: '类别名称', align: 'center'}
                ] ]
            });
        }

        //修改
        function openUpdateGoodsWindow(data) {
            mainIndex = layer.open({
                type:1,
                title:"修改类别",
                area:["500px","350px"],
                content:$("#addOrUpdateDiv"),//内容窗口
                success:function () {
                    form.val("dataFrm",data);
                    url="/goods/updategoods";
                }
            });
        }

        //删除单条数据
        function deleteGoods(data) {
            layer.confirm('是否要删除这条数据吗?', {icon: 3, title: '提示'}, function (index) {
                $.post("/goods/deleteOne", {"id": data.gid}, function (result) {
                    if (result.status) {
                        //删除成功后刷新数据表格
                        tableIns.reload();
                    }
                    layer.msg(result.message);
                }, "json");
                layer.close(index);//关闭提示框
            });
        }

    });
</script>
</body>
</html>